home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 3389 < prev    next >
Encoding:
Text File  |  1996-08-05  |  4.8 KB  |  111 lines

  1. Path: newshost.lanl.gov!tanmoy
  2. From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Please help ?!
  5. Date: 28 Jan 1996 17:07:25 GMT
  6. Organization: Los Alamos National Laboratory
  7. Message-ID: <TANMOY.96Jan28100725@qcd.lanl.gov>
  8. References: <4dm889$3hs@neptunus.pi.net> <4drnv1$cr@news.iag.net> <4drq5i$cr@news.iag.net>
  9.     <4e6hse$dvl@ns.RezoNet.NET> <310a2389.49571776@nntp.ix.netcom.com>
  10.     <4eg6h8$1db5@info.estec.esa.nl>
  11. NNTP-Posting-Host: qcd.lanl.gov
  12. Mime-Version: 1.0
  13. Content-Type: text
  14. In-reply-to: 328pt@SPOD2.dev.esoc.esa.de's message of 28 Jan 1996 15:59:36 GMT
  15.  
  16. In article <4eg6h8$1db5@info.estec.esa.nl> 328pt@SPOD2.dev.esoc.esa.de
  17. (Phil Tregoning) writes: 
  18. <snip>
  19.    >> [...] adding a cast to a pointer of the same 
  20.    >> type to the malloc return is the *safest* thing you can do:
  21.    >> 
  22.    >>   fred = malloc(n * sizeof(int));
  23.    >> 
  24.    >> Oops - fred isn't an "int *" it's a "long *", but the compiler wont 
  25.    >> issue any warnings, but in:
  26.    >> 
  27.    >>   fred = (int *)malloc(n * sizeof(int));
  28.    >> 
  29.    >> the compiler will issue an error.
  30. <snip>
  31.    I've also frequently seen the advice not to cast the return from malloc()
  32.    as it supposedly can hide errors, and have followed this advice. However,
  33.    having read what Ray Dunn wrote I thought I would try it out on my compiler
  34.    (DEC C - with default warnings) to see which cases get picked up.
  35.  
  36.    Missing out #include <stdlib.h>, and with a cast on malloc() gives a
  37.    warning and information message as follows :
  38.  
  39.        long_ptr = (int *)malloc(sizeof(int));
  40.        ..................^
  41.    %CC-I-IMPLICITFUNC, In this statement, the identifier "malloc" is implicitly
  42.     declared as a function.
  43.  
  44.        long_ptr = (int *)malloc(sizeof(int));
  45.        ^
  46.    %CC-W-PTRMISMATCH, In this statement, the referenced type of the pointer value
  47.     "(int ...)malloc(...)" is "int", which is not compatible with "long".
  48.  
  49.    I would call that fair warning that something is amiss.
  50.  
  51.    Including #include <stdlib.h>, and without a cast on malloc() gives no
  52.    messages at all on "long_ptr = malloc(sizeof(int));" because a void pointer
  53.    (returned from malloc()) is being converted to a long pointer, which is
  54.    OK as far as the compiler is concerned.
  55.  
  56.    Given the results of this, I think a may start casting the return from
  57.    malloc().
  58.  
  59. This issue keeps coming up in this newsgroup, and as any stylistic
  60. question, will probably never be `finally answered'. On compilers
  61. which warn on undeclared functions, avoiding the cast has little
  62. benefit in this particular case. One must keep in mind, however, that
  63. programming is more a matter of habit: and, often, it is best not to
  64. become too used to facilities one has at hand on a particular day on a
  65. particular platform. The compiler _need not_ warn about an undeclared
  66. function (i.e. such a warning is not mandated by ANSI: no warning is
  67. however ever prohibited). Converting an int to a pointer without a
  68. cast, however, needs a warning. On such compilers, then, avoiding the
  69. cast can often help spot a fatal error.
  70.  
  71. The point that Ray brings up is also one discussed here quite
  72. often. The first thing is that writing 
  73.  
  74. long_ptr = malloc (sizeof (int));
  75.  
  76. is inherently more error prone than writing
  77.  
  78. long_ptr = malloc (sizeof*long_ptr);
  79.  
  80. This second form is useful to learn as an idiom, and avoids most of
  81. the errors that the explicit cast is trying to flag. More importantly,
  82. a malloc is usually found close to the declaration, or close to its
  83. use: and this kind of mistake (i.e. wrong idea of what the type of a
  84. malloced variable is) usually leads to more diagnosable mistakes later
  85. anyway. As this is a statement of what kinds of codes are more common,
  86. every persons experience is likely to be different.
  87.  
  88. A related point is that a `cast' is a loophole in type-safety, just as
  89. a `goto' is a loophole in structured programming. Both have their
  90. legitimate uses, and in the hands of an expert programmer, both of
  91. them can integrate into magnificient products of art. A beginner
  92. however is often advised to stay away from a goto, I wish the same
  93. were done for casts. Unfortunately, C does not allow complete
  94. elimination of casts: but avoiding them whenever possible is probably
  95. an important lesson that every beginner needs to have.
  96.  
  97. For people here who have been programming for long, no such rules
  98. apply. As an ancient religious book from India stated, once you
  99. internalize the reason behind a rule, the rule no longer applies to
  100. you. 
  101.  
  102. Cheers
  103. Tanmoy
  104. --
  105. tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
  106. Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
  107. Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
  108. <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
  109. internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
  110. fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]
  111.